﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BOL;
using WebLib;
using System.Data;
using System.Data.Linq;
using System.Data.SqlClient;
using Website.Logger;
using UtilityExtensions;

namespace UILGestion.Seguridad
{
    public partial class RolReadForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            internalid.Value = (string)Request.QueryString["id"];
            menuCtrl1.loadMenuSeguridad();
            formatForm();
        }

        private void formatForm()
        {
            LoadRol();
        }

        private void LoadRol()
        {
            var rolInfo = DbUtil.Db.Rols.Single(p => p.idInternal == internalid.Value.ToInt());
            //rolInfo rolInfo=rol.loadRol(int.Parse(internalid.Value));

            NombreLabel.Text = rolInfo.descripcion;
            nivelPresupuestoLabel.Text = "Nivel " + rolInfo.nivelPrespuesto.ToString();
            nivelComparativoLabel.Text = "Nivel " + rolInfo.nivelComparativo.ToString();
            aprobarOCCheckBox.Checked = rolInfo.bitAprobarOrdenCompra;
            activoChk.Checked = rolInfo.activo.Value;
            fechacreacionLabel.Text = rolInfo.fechaCre.ToString();
            fechaModificacionLabel.Text = rolInfo.fechaMod.ToString();
            usuarioModLabel.Text=rolInfo.usuarioMod.ToString();
            montoLabel.Text = rolInfo.montoComparativo.Value.ToString("N");
            //--------rendicion----
            bitAprobarCheckBox.Checked = rolInfo.bitAprobarRecepcion;
            bitVistoBuenoCajaChicachk.Checked = rolInfo.bitRevisorRecepcion;
            bitDepositoCajaChicaCheckBox.Checked = rolInfo.bitDepositoRendicionC;
            //------------Kardex-----
            bitkardexCHK.Checked = rolInfo.bitAperturaKardex;
            
     
            List<rolFormularioListInfo> item = new RolBL().getRolFormTransaccion(int.Parse(internalid.Value));
            List<rolFormularioListInfo> item2 = new RolBL().getRolFormLista(int.Parse(internalid.Value));

            formularioGridView.DataSource = item;
            formularioGridView.DataBind();


            GridView1.DataSource = item2;
            GridView1.DataBind();            


        }



        protected void nuevoButton_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("~/Seguridad/RolInsertForm.aspx?type=A", false);
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
        }
        protected void btnContinue_Click(object sender, EventArgs e)
        {
            try
            {
                if (((Button)sender).ID==refreshButton.ID)
                    formatForm();

                if (((Button)sender).ID == nuevoButton.ID)
                    Response.Redirect("~/seguridad/rolInsertForm.aspx?type=A");
                if (((Button)sender).ID == editarButton.ID)
                    Response.Redirect("~/seguridad/rolInsertForm.aspx?type=E&id=" + internalid.Value);
                if (((Button)sender).ID == EliminarButton.ID)
                {
                    RolBL rol= new RolBL();
                    rol.delete(int.Parse(internalid.Value));
                    Response.Redirect("~/seguridad/rolListForm.aspx");
                }


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
            
        }
    }
}